﻿using NDK.Module.Model;
using NDK.Motion.BLL;
using NDK.Motion.MachineManager;
using NDK.Motion.Monitor;
using System;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;

namespace NDK.Module.StationLogic
{
    public class Station_下料搬运 : StationLogic
    {
        #region 通用
        public override event PropertyChangedEventHandler PropertyChanged;

        public static Station_下料搬运 Instance = new Station_下料搬运();
        public Stopwatch SunWay13 = new Stopwatch();
        public string StationName = "Station13下料搬运";


        private bool[] ArrayHoleIsHave;
        public ConcurrentQueue<bool[]> QueueHoleIsHave;

        //步骤字段、属性
        private StationStep _Step = StationStep.轴运动至安全位;
        [Monitor(Description = "当前步骤")]
        public StationStep Step
        {
            get
            {
                return _Step;
            }
            set
            {
                if (_Step != value)
                {
                    if (!Common.BP_是否屏蔽计时)
                    {
                        WriteStationCTLog("StationCTLog", $"{_Step}用时为：{swStepTime.ElapsedMilliseconds} ms");
                        swStepTime.Restart();
                    }
                    _Step = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(value.ToString()));
                }
            }
        }
        #endregion

        /// <summary>
        /// 工站初始化
        /// </summary>
        public override void Init()
        {
            base.Init();
            logBaseInfo = "下料搬运流程";
            Step = StationStep.轴运动至安全位;
            QueueHoleIsHave = new ConcurrentQueue<bool[]>();
            SunWay13.Start();
        }

        public override void ActionProcess()
        {
            try
            {
                switch (Step)
                {
                    case StationStep.轴运动至安全位:
                        if (MotionControl.AxesMove(Machine.pos_安全位_下料搬运工位))
                        {
                            SignalComplete1 = true;
                            SignalRequest = true;
                            Step = StationStep.等待上工位完成;
                        }
                        break;

                    case StationStep.等待上工位完成:
                        if (Station_产品扫码.Instance.SignalComplete)
                        {
                            Step = StationStep.轴运动至取料高位1;
                        }
                        break;

                    case StationStep.轴运动至取料高位1:
                        SignalRequest1 = false;
                        if (MotionControl.AxesMove(Machine.pos_取料高位_下料搬运工位))
                        {
                            Step = StationStep.轴移动至取料位;
                        }
                        break;

                    case StationStep.轴移动至取料位:
                        if (MotionControl.AxesMove(Machine.pos_取料位_下料搬运工位))
                        {
                            QueueHoleIsHave.TryDequeue(out ArrayHoleIsHave);
                            Step = StationStep.打开真空吸;
                        }
                        break;

                    case StationStep.打开真空吸:
                        if (VacuumMethod(true, (Machine.do_下料搬运真空吸1, Machine.di_下料搬运真空检测1, ArrayHoleIsHave[0]), (Machine.do_下料搬运真空吸2, Machine.di_下料搬运真空检测2, ArrayHoleIsHave[1])))
                        {
                            Step = StationStep.轴运动至取料高位2;
                        }
                        break;

                    case StationStep.轴运动至取料高位2:
                        if (MotionControl.AxesMove(Machine.pos_取料高位_下料搬运工位))
                        {
                            SignalRequest = false;
                            Station_PPG测量.Instance.QueueHoleIsHave.Enqueue(new bool[2] { ArrayHoleIsHave[0], ArrayHoleIsHave[1] });
                            Step = StationStep.再次判断真空吸;
                        }
                        break;


                    case StationStep.再次判断真空吸:
                        if (VacuumMethod(true, (Machine.do_下料搬运真空吸1, Machine.di_下料搬运真空检测1, ArrayHoleIsHave[0]), (Machine.do_下料搬运真空吸2, Machine.di_下料搬运真空检测2, ArrayHoleIsHave[1])))
                        {
                            Step = Station_PPG测量.Instance.SignalRequest ? StationStep.轴运动至放料高位1 : StationStep.轴运动至安全位1;
                        }
                        break;

                    case StationStep.轴运动至安全位1:
                        if (MotionControl.AxesMove(Machine.pos_安全位_下料搬运工位))
                        {
                            Step = StationStep.等待下工位要料;
                        }
                        break;

                    case StationStep.等待下工位要料:
                        if (Station_PPG测量.Instance.SignalRequest)
                        {
                            Step = StationStep.轴运动至放料高位1;
                        }
                        break;

                    case StationStep.轴运动至放料高位1:
                        SignalRequest1 = true;
                        if (MotionControl.AxesMove(Machine.pos_放料高位_下料搬运工位))
                        {
                            SignalRequest1 = false;
                            Step = StationStep.轴运动至放料位;
                        }
                        break;

                    case StationStep.轴运动至放料位:
                        if (MotionControl.AxesMove(Machine.pos_放料位_下料搬运工位))
                        {
                            Step = StationStep.关闭真空吸;
                        }
                        break;

                    case StationStep.关闭真空吸:
                        Machine.do_下料搬运真空吸1.OFF();
                        Machine.do_下料搬运真空吸2.OFF();
                        Machine.do_下料搬运真空破1.ON();
                        Machine.do_下料搬运真空破2.ON();
                        Thread.Sleep(50);
                        Machine.do_下料搬运真空破1.OFF();
                        Machine.do_下料搬运真空破2.OFF();
                        Step = StationStep.轴运动至放料高位2;
                        break;

                    case StationStep.轴运动至放料高位2:
                        if (MotionControl.AxesMove(Machine.pos_放料高位_下料搬运工位))
                        {
                            SignalComplete = true;
                            Step = StationStep.等待下工位关要料;
                        }
                        break;

                    case StationStep.等待下工位关要料:
                        if (!Station_PPG测量.Instance.SignalRequest)
                        {
                            SignalComplete = false;
                            Step = StationStep.轴运动至安全位;
                        }
                        break;


                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                UILog_Error($"[{logBaseInfo}] {ex.Message}");
            }
        }

        public override void EmptyActionProcess()
        {
            ActionProcess();
        }

        public override void GRRActionProcess()
        {
            ActionProcess();
        }

        public enum StationStep
        {
            轴运动至安全位,
            轴运动至取料高位1,
            等待上工位完成,
            轴移动至取料位,
            打开真空吸,
            轴运动至取料高位2,
            再次判断真空吸,
            轴运动至安全位1,
            轴运动至放料高位1,
            等待下工位要料,
            轴运动至放料位,
            关闭真空吸,
            轴运动至放料高位2,
            等待下工位关要料
        }
    }

}
